
*This program contains the information needed to create Table 2 of the paper;
*The program references monthly_determinants which is  constructed in  "Determinants Monthly Panel" (Intermediate File) code;

libname mylib 'g:\Dropbox\Wall Street Bets (Private)\Data';

proc sort data=mylib.monthly_determinants;
by cum_month;
run;






proc standard data=mylib.monthly_determinants out=TABLE2_FE MEAN=0;
BY CUM_MONTH;
var    LN_INST_OWN  ln_cshr   ln_size ln_bm ln_vol ln_ave_turn mom1 mom2_12     LN_MEDIA ln_ibes_coverage HEAVY_SHORT IPO6 NEG_BM MISSING_MEDIA
LN_INST_OWN21  ln_cshr21   ln_size21 ln_bm21 ln_vol21 ln_ave_turn21 mom1_21 mom2_12_21     LN_MEDIA21 ln_ibes_coverage21 HEAVY_SHORT21 IPO6_21 NEG_BM21 MISSING_MEDIA21
 high_max high_max21 very_high_max very_high_max21


log_sa_coverage log_sa_coverage2 log_dd_posts log_dd_posts2 log_non_research_posts log_non_research_posts2  gme_amc_flag gme_amc_flag_21;
run;


*this code generates specification 1 of Table2;
*for specification 2 replace log_dd_posts2 with log_non_research_posts2;
*for specification 3 replace log_dd_posts2 with log_sa_coverage2;



proc means data=table2_fe;
var log_dd_posts2 LN_INST_OWN LN_INST_OWN21 
ln_cshr  ln_cshr21  ln_size ln_size21 ln_bm ln_bm21  NEG_BM NEG_BM21 ln_vol ln_vol21 high_max high_max21 ln_ave_turn ln_ave_turn21 mom1 mom1_21 mom2_12   mom2_12_21     LN_MEDIA LN_MEDIA21
ln_ibes_coverage ln_ibes_coverage21 HEAVY_SHORT HEAVY_SHORT21 IPO6 ipo6_21  gme_amc_flag gme_amc_flag_21 MISSING_MEDIA MISSING_MEDIA21;
run;



%let yourdata=TABLE2_FE;

%let firmid=ticker ;
%let time=cum_month;

%let y=log_dd_posts2 ;
%let x= LN_INST_OWN LN_INST_OWN21 
ln_cshr  ln_cshr21  ln_size ln_size21 ln_bm ln_bm21  NEG_BM NEG_BM21 ln_vol ln_vol21 high_max high_max21 ln_ave_turn ln_ave_turn21 mom1 mom1_21 mom2_12   mom2_12_21     LN_MEDIA LN_MEDIA21
ln_ibes_coverage ln_ibes_coverage21 HEAVY_SHORT HEAVY_SHORT21 IPO6 ipo6_21  gme_amc_flag gme_amc_flag_21 MISSING_MEDIA MISSING_MEDIA21;
RUN;





*cluster by first dimension (e.g., firm);
proc surveyreg data=&yourdata;

    cluster &firmid;
    model &y = &x /covb;
    ods output covb=firm;
run;
quit;


*cluster by second dimension (e.g., year);
proc surveyreg data=&yourdata;

    cluster &time;
    model &y = &x /covb;
    ods output covb=year;
run;
quit;

*cluster by intersection of the two dimensions (e.g, firm-year);
proc surveyreg data=&yourdata;
    cluster &firmid &time;
    model &y = &x /covb;
    ods output covb=both;
    ods output parameterestimates=parm;
run;
quit;

*keeps original parameter estimates;
data parm; set parm;
    keep parameter estimate;
run;

*returns a dataset with a scalar for the dimensions of the var/cov matrix. This is needed to extract the square roots of the diagonals later on;
data parm1; set parm;
    n=_n_;
    m=1;
    keep m n;
run;

data parm1; set parm1; by m;
    if last.m;
    keep n;
run;

*uses matrix algebra interface to construct Var-cov matrix and extract the standard errors;
proc iml;
    use both;
    read all var _num_ into Z;
    print Z;
    use firm;
    read all var _num_ into X;
    print X;
    use year;
    read all var _num_ into Y;
    print Y;
    use parm1;
    read all var _num_ into n;
    print n;

    B=X+Y-Z;
    C=I(n);
    D=J(n,1);
    E=C#B;
    F=E*D;
    G=F##.5;

    print B;
    print G;
    create b from G [colname='stderr']; ;
    append from G;
quit;

*creates a dataset called 'results' that contains the parameter estimates, the SE's, and the t-stats;
data results; merge parm B;
    tstat=estimate/stderr;
run;

proc print data=results;
run;




data spec3;
set results;
estimate3 = estimate  ;
obs = _n_;
keep parameter estimate3 obs;
run;
data spec3b;
set results;
estimate3 = tstat ;
obs = _n_ + .5;
keep parameter estimate3 obs;
run;
data spec3;
set spec3 spec3b;
run;
proc sort data=spec3;
by obs;
run;

